package wiki.kaizen.cloud.plugin.spring.deserializer;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.time.temporal.Temporal;

/**
 *  Temporal 日期时间 反序列化抽象类
 *
 * @author xeepoo
 * */
@SuppressWarnings("unchecked")
public abstract class AbstractLocalTemporalDeserializer implements ObjectDeserializer {
    @Override
    public Temporal deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        String key;
        if (fieldName==null|| StringUtils.isEmpty((key = (String)fieldName))){
            return Jdk8DateCodec.instance.deserialze(parser,type,null);
        }

        Object value = JSONObject.parseObject(parser.getInput()).get(key);

        /*
        *具体逻辑与处理结果交由子类处理
        * */
        return this.produce(value);
    }
    //localDate     Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
    //localdatetime Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
    //localTime     Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
    protected abstract  Temporal produce(Object value);

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
